/**
 * 
 */
package org.lora.biz.ou.group;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.lora.biz.ou.LoraOUConstant;
import org.lora.biz.ou.entity.EmployeeIdentityView;
import org.lora.biz.ou.exception.LoraOrgUserException;
import org.lora.biz.ou.group.entity.Group;
import org.lora.biz.ou.group.entity.GroupFormula;
import org.lora.biz.ou.group.service.IGroupService;
import org.lora.core.annotation.Action;
import org.lora.core.annotation.AutoAssemble;
import org.lora.core.mybatis.Page;
import org.lora.mvc.annotation.RequestPath;
import org.lora.mvc.annotation.ResponseJSON;
import org.lora.mvc.entity.ViewData;
import org.lora.webframework.common.CommonConstant;
import org.lora.webframework.common.JsonResult;
import org.lora.webframework.common.WebframeBaseAction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

/**
 * <p>
 * 类名:GroupManageAction
 * </p>
 * 描述:群组管理Action<br>
 * 创建时间:2016年9月22日 下午3:56:39<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年9月22日 Bladnir 创建<br>
 */
@Action
@RequestPath(path = "/biz/ou/group")
public class GroupManageAction extends WebframeBaseAction {

	@AutoAssemble
	private IGroupService groupService;

	/**
	 * 跳转到群组管理页面
	 * 
	 * @return
	 */
	@RequestPath(path = "/showGroupManage")
	public ViewData showGroupManage() {
		return new ViewData("/jsp/ou/groupManage.jsp");
	}

	/**
	 * 打开群组树形选择页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showGroupTree")
	public ViewData showGroupTree(HttpServletRequest request) {
		Map<String, Object> dataMap = new HashMap<>();
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);
		return new ViewData("/jsp/ou/groupTreeSelect.jsp", dataMap);
	}

	/**
	 * 打开添加身份到群组页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showAddEIToGroup")
	public ViewData showAddEIToGroup(HttpServletRequest request) {
		Map<String, Object> dataMap = new HashMap<>();
		String groupId = request.getParameter(LoraOUConstant.GROUP_ID_KEY);
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);

		dataMap.put(LoraOUConstant.GROUP_ID_KEY, groupId);
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);

		return new ViewData("/jsp/ou/groupEIAdd.jsp", dataMap);
	}

	/**
	 * 打开添加公式到群组页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showAddFormula")
	public ViewData showAddFormula(HttpServletRequest request) {
		Map<String, Object> dataMap = new HashMap<>();
		String groupId = request.getParameter(LoraOUConstant.GROUP_ID_KEY);
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);

		dataMap.put(LoraOUConstant.GROUP_ID_KEY, groupId);
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);

		return new ViewData("/jsp/ou/groupFormulaAdd.jsp", dataMap);
	}

	/**
	 * 打开群组预览页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showGroupPreview")
	public ViewData showGroupPreview(HttpServletRequest request) {
		Map<String, Object> dataMap = new HashMap<>();
		String groupId = request.getParameter(LoraOUConstant.GROUP_ID_KEY);
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);

		dataMap.put(LoraOUConstant.GROUP_ID_KEY, groupId);
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);

		return new ViewData("/jsp/ou/groupPreview.jsp", dataMap);
	}

	/**
	 * 打开添加群组页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showGroupAdd")
	public ViewData showGroupAdd(HttpServletRequest request) {
		Map<String, Object> dataMap = new HashMap<>();
		String parentId = request.getParameter("parentId");
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		String type = request.getParameter("type");

		dataMap.put("parentId", parentId);
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);
		dataMap.put("type", type);

		return new ViewData("/jsp/ou/groupAdd.jsp", dataMap);
	}

	/**
	 * 获取群组树形数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getGroupListData")
	@ResponseJSON
	public Object getGroupListData(HttpServletRequest request) {

		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		List<Group> groupList = new ArrayList<>();
		try {
			groupList = groupService.getGroupListByDomainId(domainId);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return groupList;

	}

	/**
	 * 获取群组预览数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getGroupPreviewListData")
	@ResponseJSON
	public Object getGroupPreviewListData(HttpServletRequest request) {
		String groupId = request.getParameter(LoraOUConstant.GROUP_ID_KEY);
		String takePartId = request.getParameter("takePartId");

		Map<String, Object> resultMap = new HashMap<>();

		try {
			List<EmployeeIdentityView> list = groupService.getGroupMember(groupId, takePartId, takePartId, null);
			packageLoraTablegridData(list, list.size(), resultMap);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return resultMap;
	}

	/**
	 * 获取群组显示的内容
	 * 
	 * 动态计算群组 显示群组对应的公式信息 固定人员群组 显示群组中的身份
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getGroupContent")
	@ResponseJSON
	public Object getGroupContent(HttpServletRequest request) {

		String groupId = request.getParameter(LoraOUConstant.GROUP_ID_KEY);
		String groupType = request.getParameter("groupType");

		Map<String, Object> resultMap = new HashMap<>();

		try {

			// 设置分页Bean
			Page page = super.getPageBean(request);

			if (groupType.equals(LoraOUConstant.GROUP_TYPE_FIXED)) {
				List<EmployeeIdentityView> list = groupService.getGroupMember(groupId, null, null, page);
				int count = groupService.getGroupMemberCount(groupId, null, null);

				packageLoraTablegridData(list, count, resultMap);
			} else if (groupType.equals(LoraOUConstant.GROUP_TYPE_CALCULATE)) {
				List<GroupFormula> list = groupService.getGroupFormulaList(groupId, page);
				int count = groupService.getGroupFormulaListCount(groupId);

				packageLoraTablegridData(list, count, resultMap);
			}

		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return resultMap;

	}

	/**
	 * 批量添加身份到群组
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/addEIToGroup")
	@ResponseJSON
	public JsonResult addEIToGroup(HttpServletRequest request) {
		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);

		String groupId = request.getParameter(LoraOUConstant.GROUP_ID_KEY);
		String jsonData = request.getParameter("jsonData");

		try {
			JSONArray eiIdArray = JSON.parseArray(jsonData);
			List<String> eiIdList = new ArrayList<>();
			for (int i = 0; i < eiIdArray.size(); i++) {
				eiIdList.add(eiIdArray.get(i).toString());
			}
			groupService.addEIToGroup(groupId, eiIdList);

			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	/**
	 * 批量删除群组中的身份
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/deleteEIFromGroup")
	@ResponseJSON
	public JsonResult deleteEIFromGroup(HttpServletRequest request) {
		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);

		String groupId = request.getParameter(LoraOUConstant.GROUP_ID_KEY);
		String jsonData = request.getParameter("jsonData");

		try {
			JSONArray eiIdArray = JSON.parseArray(jsonData);
			List<String> eiIdList = new ArrayList<>();
			for (int i = 0; i < eiIdArray.size(); i++) {
				eiIdList.add(eiIdArray.get(i).toString());
			}
			groupService.deleteEIToGroup(groupId, eiIdList);

			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	/**
	 * 批量添加身份到群组
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/addFormulaToGroup")
	@ResponseJSON
	public JsonResult addFormulaToGroup(HttpServletRequest request) {
		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);

		String jsonData = request.getParameter("jsonData");

		try {
			GroupFormula groupFormula = JSON.parseObject(jsonData, GroupFormula.class);
			groupService.insertGroupFormula(groupFormula);

			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	/**
	 * 从群组中批量删除公式
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/deleteFormulaFromGroup")
	@ResponseJSON
	public JsonResult deleteFormulaFromGroup(HttpServletRequest request) {
		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);

		String jsonData = request.getParameter("jsonData");

		try {
			JSONArray idArray = JSON.parseArray(jsonData);
			List<String> idList = new ArrayList<>();
			for (int i = 0; i < idArray.size(); i++) {
				idList.add(idArray.get(i).toString());
			}
			groupService.deleteGroupFormulaById(idList);

			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	/**
	 * 保存群组和类型
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/saveGroupAndType")
	@ResponseJSON
	public JsonResult saveGroupAndType(HttpServletRequest request) {
		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);

		String jsonData = request.getParameter("jsonData");

		try {
			Group group = JSON.parseObject(jsonData, Group.class);
			groupService.addGroupAndType(group);
			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	/**
	 * 删除群组或者群组的类型
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/deleteGroup")
	@ResponseJSON
	public JsonResult deleteGroup(HttpServletRequest request) {
		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);

		String groupId = request.getParameter("groupId");
		try {
			groupService.deleteGroupById(groupId);
			result.setFlag(CommonConstant.SUCCESS);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

}
